'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class Snac1308
    Inherits Snac

    Public Sub New()
        MyBase.New(&H13, &H8)
    End Sub

    Public Overrides Function CalculateDataSize() As Integer
        Dim size As Integer
        For Each x As SSIRecord In _BuddyRecords
            size += x.CalculateTotalSize
        Next
        For Each x As SSIRecord In _GroupRecords
            size += x.CalculateTotalSize
        Next
        For Each x As SSIRecord In _PermitRecords
            size += x.CalculateTotalSize
        Next
        For Each x As SSIRecord In _DenyRecords
            size += x.CalculateTotalSize
        Next
        For Each x As SSIRecord In _IgnoreListRecords
            size += x.CalculateTotalSize
        Next
        If BuddyIcon IsNot Nothing Then
            size += BuddyIcon.CalculateTotalSize
        End If
        Return size
    End Function

    Private _BuddyRecords As New List(Of SSIBuddyRecord)
    Public ReadOnly Property BuddyRecords() As List(Of SSIBuddyRecord)
        Get
            Return _BuddyRecords
        End Get
    End Property

    Private _GroupRecords As New List(Of SSIGroupRecord)
    Public ReadOnly Property GroupRecords() As List(Of SSIGroupRecord)
        Get
            Return _GroupRecords
        End Get
    End Property

    Private _PermitRecords As New List(Of SSIPermitRecord)
    Public ReadOnly Property PermitRecords() As List(Of SSIPermitRecord)
        Get
            Return _PermitRecords
        End Get
    End Property

    Private _IgnoreListRecords As New List(Of SSIIgnoreListRecord)
    Public ReadOnly Property IgnoreListRecords() As List(Of SSIIgnoreListRecord)
        Get
            Return _IgnoreListRecords
        End Get
    End Property

    Private _DenyRecords As New List(Of SSIDenyRecord)
    Public ReadOnly Property DenyRecords() As List(Of SSIDenyRecord)
        Get
            Return _DenyRecords
        End Get
    End Property

    Private _BuddyIcon As SSIBuddyIcon
    Public Property BuddyIcon() As SSIBuddyIcon
        Get
            Return _BuddyIcon
        End Get
        Set(ByVal value As SSIBuddyIcon)
            _BuddyIcon = value
        End Set
    End Property

    Private _MaxItemId As Integer
    Public ReadOnly Property MaxItemId() As Integer
        Get
            Return _MaxItemId
        End Get
    End Property

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Snac.SizeFixPart

        Do While index < data.Count
            Dim desc As SSIItemDescriptor = SSIItemDescriptor.GetDescriptor(index, data)

            Select Case desc.ItemType
                Case SSIItemType.BuddyRecord
                    Dim buddy As SSIBuddyRecord
                    buddy = New SSIBuddyRecord
                    buddy.Deserialize(data.GetRange(index, desc.TotalSize))
                    _BuddyRecords.Add(buddy)
                Case SSIItemType.GroupRecord
                    Dim group As SSIGroupRecord
                    group = New SSIGroupRecord
                    group.Deserialize(data.GetRange(index, desc.TotalSize))
                    _GroupRecords.Add(group)
                Case SSIItemType.PermitRecord
                    Dim permit As SSIPermitRecord
                    permit = New SSIPermitRecord
                    permit.Deserialize(data.GetRange(index, desc.TotalSize))
                    _PermitRecords.Add(permit)
                Case SSIItemType.DenyRecord
                    Dim deny As SSIDenyRecord
                    deny = New SSIDenyRecord
                    deny.Deserialize(data.GetRange(index, desc.TotalSize))
                    _DenyRecords.Add(deny)
                Case SSIItemType.IgnoreListRecord
                    Dim ignore As SSIIgnoreListRecord
                    ignore = New SSIIgnoreListRecord
                    ignore.Deserialize(data.GetRange(index, desc.TotalSize))
                    _IgnoreListRecords.Add(ignore)
                Case Else
                    Core.Kernel.Logger.Log("Snac1308", TraceEventType.Error, "Unsupported SSI item type: {0}", desc.ItemType)
            End Select

            index += desc.TotalSize

            _MaxItemId = Math.Max(desc.ItemId, MaxItemId)
        Loop

        Me.SetTotalSize(index)
    End Sub

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        For Each x As SSIRecord In _BuddyRecords
            data.AddRange(x.Serialize)
        Next
        For Each x As SSIRecord In _GroupRecords
            data.AddRange(x.Serialize)
        Next
        For Each x As SSIRecord In _PermitRecords
            data.AddRange(x.Serialize)
        Next
        For Each x As SSIRecord In _DenyRecords
            data.AddRange(x.Serialize)
        Next
        For Each x As SSIRecord In _IgnoreListRecords
            data.AddRange(x.Serialize)
        Next
        If BuddyIcon IsNot Nothing Then
            data.AddRange(BuddyIcon.Serialize)
        End If

        Return data
    End Function
End Class
